package com.itheima.controller.impl;

import com.itheima.controller.IStudentController;
import com.itheima.domain.Student;
import com.itheima.factory.Factory;
import com.itheima.service.IStudentService;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentControllerImpl implements IStudentController {
    private IStudentService studentService=Factory.getStudentService();
    private Scanner sc= new Scanner(System.in);

    @Override
    public void start() {
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("新增学生");
                    add();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    delete();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    update();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAll();
                    break;
                case "5":
                    System.out.println("感谢使用学生系统");
                    return;
                default:
                    System.out.println("请输入正确的选择");
            }
        }
    }

    @Override
    public void add() {
        String sid = null;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            boolean exist = studentService.isExist(sid);
            if(exist){
                System.out.println("已经存在,请重新录入");
            }else {
               break;
            }
        }

        Student student = getStudentInfo(sid);
        studentService.add(student);
    }



    @Override
    public void delete() {
        ArrayList<Student> list = studentService.findAll();
        if(list==null||list.size()==0){
            System.out.println("没有数据请先添加");
            return;
        }
        String sid = getStudentSid();
        studentService.delete(sid);
    }



    @Override
    public void update() {
        ArrayList<Student> list = studentService.findAll();
        if(list==null||list.size()==0){
            System.out.println("没有数据请先添加");
            return;
        }
        String sid = getStudentSid();
        Student student = getStudentInfo(sid);
        studentService.update(sid,student);
    }

    @Override
    public void findAll() {
        ArrayList<Student> list = studentService.findAll();
        if(list==null||list.size()==0){
            System.out.println("没有数据请先添加");
            return;
        }
        System.out.println(list);
    }

    private Student getStudentInfo(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();

        return new Student(sid, name, age, birthday);
    }
    private String getStudentSid() {
        String sid = null;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            boolean exist = studentService.isExist(sid);
            if(exist){
                break;
            }else {
                System.out.println("已经存在,请重新录入");

            }
        }
        return sid;
    }
}
